package com.day12.study.netcode_demo;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpCookie;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo {
    public static void main(String[] args) {
        URL url = null;
        try {
            url = new URL("www.baidu.com");
            URLConnection urlConnection = url.openConnection();
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) urlConnection;
            InputStream in = httpsURLConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in, "utf-8");
            int i = reader.read();
            while (i != -1) {
                System.out.println((char) i);
                i = reader.read();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(url.getHost());
        System.out.println(url.getPort());
    }
}
